﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Storage;
using System.IO;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;

using _XNA__RunningDeath.GLO;
using _XNA__RunningDeath.BUS.VatThe;

namespace _XNA__RunningDeath.DAO
{


    public struct SaveScore
    {
        public string PlayerName;
        public int Score;
        public string fileName;
    }

    class CSaveScore
    {
        static IAsyncResult asyncResult;
        static StorageDevice storageDevice;
        static PlayerIndex playerIndex = PlayerIndex.One;
        static SaveScore noData;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"> "savegame.sav"</param>
        /// <param name="data">Dữ Liệu truyền vào</param>
        public static bool doSave(string filename, SaveScore data)
        {
            try
            {
                asyncResult = StorageDevice.BeginShowSelector(playerIndex, null, null);
                storageDevice = StorageDevice.EndShowSelector(asyncResult);

                IAsyncResult result = storageDevice.BeginOpenContainer("SaveScore", null, null);
                result.AsyncWaitHandle.WaitOne();
                StorageContainer container = storageDevice.EndOpenContainer(result);

                result.AsyncWaitHandle.Close();

                if (container != null)
                {
                    if (container.FileExists(filename))
                    {
                        container.DeleteFile(filename);
                    }

                    string[] arrFileName = container.GetFileNames();
                    if( arrFileName.Count() > 5)
                        doRemoveMin();

                    using (Stream stream = container.CreateFile(filename))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(SaveScore));
                        serializer.Serialize(stream, data);
                        stream.Close();
                    }
                }
                container.Dispose();
                container = null;

                return true;
            }
            catch
            {
                return false;
            }
        }

        private static void doRemoveMin()
        {
            loadArrData();
            List<SaveScore> dsScore = CGLOBAL.arrScoreGame;

            int min = 0;
            for (int i = 1; i < dsScore.Count; i++)
            {
                if (dsScore[i].Score <= dsScore[min].Score)
                {
                    min = i;
                }
            }
            
            doDelete(dsScore[min].fileName);
            dsScore.RemoveAt(min);
            CGLOBAL.arrScoreGame = dsScore;

            
        }

        public static void doDelete(string filename)
        {
            try
            {
                asyncResult = StorageDevice.BeginShowSelector(playerIndex, null, null);
                storageDevice = StorageDevice.EndShowSelector(asyncResult);

                IAsyncResult result = storageDevice.BeginOpenContainer("SaveScore", null, null);
                result.AsyncWaitHandle.WaitOne();
                StorageContainer container = storageDevice.EndOpenContainer(result);

                result.AsyncWaitHandle.Close();

                if (container != null)
                {
                    if (container.FileExists(filename))
                    {
                        container.DeleteFile(filename);
                    }
                }
                container.Dispose();
                container = null;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Đọc và trả về Dữ liệu game đã lưu
        /// </summary>
        /// <param name="filename">Tên file đã lưu</param>
        /// <returns></returns>
        public static SaveScore doLoad(string filename)
        {
            try
            {
                asyncResult = StorageDevice.BeginShowSelector(playerIndex, null, null);
                storageDevice = StorageDevice.EndShowSelector(asyncResult);

                IAsyncResult result = storageDevice.BeginOpenContainer("SaveScore", null, null);
                result.AsyncWaitHandle.WaitOne();
                StorageContainer container = storageDevice.EndOpenContainer(result);

                result.AsyncWaitHandle.Close();

                string[] file = container.GetFileNames();

                foreach (string item in file)
                {

                }

                if (container != null)
                {
                    if (!container.FileExists(filename))
                    {
                        // If not, dispose of the container and return.
                        container.Dispose();
                    }
                    else
                    {

                        // Open the file.
                        Stream stream = container.OpenFile(filename, FileMode.Open);

                        // Convert the object to XML data and put it in the stream.
                        XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));

                        SaveScore data = (SaveScore)serializer.Deserialize(stream);

                        stream.Close();

                        // Dispose the container, to commit the data.
                        container.Dispose();
                        return data;
                    }
                }
                container.Dispose();
                container = null;
            }
            catch
            {
            }
            noData.Score = -1;
            return noData;
        }

        public static void loadArrData()
        {
            CGLOBAL.arrScoreGame.Clear();
            try
            {
                asyncResult = StorageDevice.BeginShowSelector(playerIndex, null, null);
                storageDevice = StorageDevice.EndShowSelector(asyncResult);

                IAsyncResult result = storageDevice.BeginOpenContainer("SaveScore", null, null);
                result.AsyncWaitHandle.WaitOne();
                StorageContainer container = storageDevice.EndOpenContainer(result);

                result.AsyncWaitHandle.Close();

                if (container != null)
                {
                    string[] arrFileName = container.GetFileNames();

                    foreach (string filename in arrFileName)
                    {
                        if (!container.FileExists(filename))
                        {
                            container.Dispose();
                            return;
                        }
                        else
                        {
                            Stream stream = container.OpenFile(filename, FileMode.Open);

                            XmlSerializer serializer = new XmlSerializer(typeof(SaveScore));

                            SaveScore data = (SaveScore)serializer.Deserialize(stream);
                            data.fileName = filename;

                            CGLOBAL.arrScoreGame.Add(data);

                            stream.Close();
                        }
                    }
                    container.Dispose();
                    container = null;
                }
                else
                {
                    container.Dispose();
                    container = null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static void doOpenFile(StorageDevice device, string filename)
        {
            // Open a storage container.
            IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null);
            //Chờ đợi cho việc chờ xử lý để trở thành tín hiệu. Wait for the Wait Handle to become signaled.
            result.AsyncWaitHandle.WaitOne();

            StorageContainer container = device.EndOpenContainer(result);
            // Close the wait handle.
            result.AsyncWaitHandle.Close();

            // Add the container path to our file name.
            filename = "savegame.sav";
            // Check to see whether the save exists.
            if (!container.FileExists(filename))
            {
                // If not, dispose of the container and return.
                container.Dispose();
                return;
            }

            // Open the file.
            Stream stream = container.OpenFile(filename, FileMode.Open);

            // Convert the object to XML data and put it in the stream.
            XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));

            SaveGameData data = (SaveGameData)serializer.Deserialize(stream);

            stream.Close();

            // Dispose the container, to commit the data.
            container.Dispose();
        }
    }
}
